package vashion.azeroth.back.controller.tran;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.core.manager.personal.tran.PersonalDisburseManager;
import vashion.azeroth.core.personal.tran.pojo.PersonalDisburse;
import vashion.azeroth.core.personal.tran.pojo.PersonalDisburseExt;
import vashion.azeroth.core.personal.tran.pojo.PersonalSnapshotSku;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.base.BaseService;
import vashion.azeroth.core.service.tran.PersonalDisburseService;
import vashion.azeroth.core.service.tran.PingCallbackService;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/back/tran")
public class DisburseController {

	@Autowired
	private PersonalDisburseService disburseService;
	@Autowired
	private PersonalDisburseManager disburseManager;
	@Autowired
	private PingCallbackService pingCallbackService;
	
    @RequestMapping("getDisburseList.htm")
    @ResponseBody
    public ApiResult<List<PersonalDisburseExt>> getDisburseList(@ModelAttribute("PersonalDisburse") PersonalDisburseExt disburse) {

        return disburseService.getDisburseList(disburse);
    }
    
    @RequestMapping("queryDisburseOrder.htm")
    public String getDisburseList(Model model, @ModelAttribute("PersonalDisburse") PersonalDisburseExt disburse) {
    	
    	if(null != disburse.getStatus() && disburse.getStatus() == -1) {
    		disburse.setStatus(null);
    	}
		int totalCount = disburseManager.getDisburseListCnt(disburse);
		BaseService page = new BaseService();
		page.initPages(disburse, totalCount);
		
		List<PersonalDisburseExt> disburseOrderList = disburseService.queryDisburseList(disburse);
		
		for (PersonalDisburseExt personalDisburseExt : disburseOrderList) {
			personalDisburseExt.setSkus(JSON.parseArray(personalDisburseExt.getSnapshot().getProductQuantity(), PersonalSnapshotSku.class));
			
			List<PersonalSnapshotSku> skus = personalDisburseExt.getSkus();
			int itemCount = 0;
			for (PersonalSnapshotSku sku : skus) {
				itemCount += sku.getStock();
			}
			personalDisburseExt.getSnapshot().setQuantity(itemCount);
		}
		
		model.addAttribute("disburseOrderList", disburseOrderList);
		model.addAttribute("disburse", disburse);
		model.addAttribute("page", page);

        return "/order/disburseOrderList";
    }
    
    @RequestMapping("disburseOrderModify.htm")
    @ResponseBody
    public ApiResult<String> disburseOrderModify(String disburseNo, String refuseReason) {
    	return pingCallbackService.confirmPayed(disburseNo, refuseReason);
    }
    
    @RequestMapping("getDisburseOrder.htm")
    public String getDisburseOrder(Model model, String disburseNo) {
        
    	PersonalDisburse disburse = disburseManager.getByDisburseNo(disburseNo);
    	disburse.setSkus(JSON.parseArray(disburse.getSnapshot().getProductQuantity(), PersonalSnapshotSku.class));
    	model.addAttribute("disburse", disburse);
        return "/order/modifyDisburseOrder";
    }
    
    @RequestMapping("sendGoods.htm")
    @ResponseBody
    public ApiResult<String> sendGoods(PersonalDisburse disburse) {
    	
    	return disburseService.sendGoods(disburse);
    }
    
    
    
}
